package com.example.demo.poi;

import com.alibaba.fastjson.JSON;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ExcelTest {
    public static void main(String[] args) {
        try {
            boolean hasImage = hasImage("F:\\360MoveData\\Users\\lqq\\Desktop\\image\\系统端口梳理.xlsx");
            if (hasImage) {
                System.out.println("Excel中存在图片");
            } else {
                System.out.println("Excel中不存在图片");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static boolean hasImage(String filePath) throws IOException {
        InputStream inputStream = new FileInputStream(filePath);
        Workbook workbook;
        if (filePath.endsWith("xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else if (filePath.endsWith("xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else {
            throw new IllegalArgumentException("Unsupported file format");
        }

        boolean hasImage = false;
        int numberOfSheets = workbook.getNumberOfSheets();
        Set<CellType> map=new HashSet<>();
        for (int i = 0; i < numberOfSheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    map.add(cell.getCellType());
//                    if (cell.getCellType() == CellType.PICTURE) {
//                        hasImage = true;
//                        break;
//                    }
                }
            }
        }
        System.out.println(JSON.toJSONString(map));
        workbook.close();
        inputStream.close();

        return hasImage;
    }
}
